package com.sec.etech.bpm.cust.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.core.util.StringUtil;
import com.sec.etech.bpm.cust.dao.EtechTaskDao;
import org.openbpm.bpm.core.manager.BpmTaskManager;
import org.openbpm.bpm.core.manager.TaskIdentityLinkManager;
import org.openbpm.bpm.core.manager.impl.BpmTaskManagerImpl;
import org.openbpm.bpm.core.model.BpmTask;
import org.openbpm.form.util.JdbcTemplateUtil;
import org.openbpm.sys.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Primary
@Service("etechTaskManager")
public class EtechTaskManagerImpl extends BpmTaskManagerImpl implements BpmTaskManager {

    @Autowired
    EtechTaskDao etechTaskDao;

    @Resource
    TaskIdentityLinkManager taskIdentityLinkManager;

    public List getTodoList2(String userId, QueryFilter queryFilter) {
        Set<String> userRights = this.taskIdentityLinkManager.getUserRights(userId);
        queryFilter.addParamsFilter("userRights", userRights);
        queryFilter.addParamsFilter("userId", ContextUtil.getCurrentUserId());
        return etechTaskDao.getTodoList2(queryFilter);
    }

    public List getTodoList2(QueryFilter queryFilter) {
        String type = (String)queryFilter.getParams().get("type");
        // 班组待处理
        if ("todo4managerGroup".equals(type)) {
            String[] groupIds = ContextUtil.getCurrentGroupIds();
            queryFilter.addParamsFilter("userGroupIds", Arrays.asList(groupIds));
            return etechTaskDao.getTodoList4masterGroup(queryFilter);
        }
        return etechTaskDao.getTodoList2(queryFilter);
    }


    public List getTodoListBak(QueryFilter queryFilter) {
        String type = (String)queryFilter.getParams().get("type");
        // 班组待处理
        if ("todo4managerGroup".equals(type)) {
            String userId = ContextUtil.getCurrentUserId();
            String sql = "select distinct task.* " +
                    " from bpm_task task left join bpm_task_opinion o on o.task_id_ = task.task_id_ " +
                    " where o.master_group_ IN ('"+convetToInStr(ContextUtil.getCurrentGroupIds())+"') ";
            if(queryFilter.getPage()!=null && queryFilter.getPage().getPageSize()>0){
                int start = queryFilter.getPage().getStartIndex();
                int limit = queryFilter.getPage().getPageSize();
                sql += " limit "+start+","+limit;
            }
            JdbcTemplate jdbcTemplate = JdbcTemplateUtil.getJdbcTemplate("dataSourceDefault");
            return covertTasks(jdbcTemplate.queryForList(sql));
        }
        return super.getTodoList(queryFilter);
    }

    private static List<BpmTask> covertTasks(List<Map<String, Object>> listData){
        List<BpmTask> tasks = new ArrayList<BpmTask>();
        if(listData!=null && !listData.isEmpty()){
            for(Map row: listData){
                BpmTask task = new BpmTask();
                BeanUtil.copyProperties(row, task);
                task.setId(MapUtil.getStr(row, "id_"));
                task.setTaskId(MapUtil.getStr(row, "task_id_"));
                task.setInstId(MapUtil.getStr(row, "inst_id_"));
                task.setSubject(MapUtil.getStr(row, "subject_"));
                task.setName(MapUtil.getStr(row, "name_"));
                task.setTaskType(MapUtil.getStr(row, "task_type_"));
                task.setStatus(MapUtil.getStr(row, "status_"));
                task.setCreateTime(MapUtil.getDate(row, "create_time_"));
                task.setExtData(MapUtil.getStr(row, "ext_data_"));
                tasks.add(task);
            }
        }
        return tasks;

    }

    private static String convetToInStr(Collection<String> values) {
        return convetToInStr(values.toArray(new String[values.size()]));
    }
    private static String convetToInStr(String[] values) {
        return StringUtil.join(values, "','");
    }
}
